using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace zevenseas.Community.Web.UI.WebControls.WebParts
{
    public class QuickAlertEditor : EditorPart
    {
        private Panel toolPanePanel;
        private ListDropDown listDropDown;
        private DropDownList frequencyDropDown;
        private DropDownList changeTypeDropDown;
        private TextBox createAlertText;
        private TextBox deleteAlertText;

        protected override void CreateChildControls()
        {
            toolPanePanel = new Panel();

            Title = "Quick Alert Configuration";
            toolPanePanel = new Panel();
            toolPanePanel.Attributes.CssStyle.Add("padding-left", "5px");
            toolPanePanel.Attributes.CssStyle.Add("padding-top", "5px");
            Controls.Add(toolPanePanel);

            toolPanePanel.Controls.Add(new LiteralControl("List:</br>"));

            listDropDown = new ListDropDown();
            toolPanePanel.Controls.Add(listDropDown);

            toolPanePanel.Controls.Add(new LiteralControl("</br>Change Type:</br>"));
            changeTypeDropDown = new DropDownList();
            changeTypeDropDown.DataSource = Enum.GetNames(typeof(SPEventType));
            changeTypeDropDown.DataBind();
            toolPanePanel.Controls.Add(changeTypeDropDown);

            toolPanePanel.Controls.Add(new LiteralControl("</br>Frequency:</br>"));
            frequencyDropDown = new DropDownList();
            frequencyDropDown.DataSource = Enum.GetNames(typeof(SPAlertFrequency));
            frequencyDropDown.DataBind();
            toolPanePanel.Controls.Add(frequencyDropDown);

            toolPanePanel.Controls.Add(new LiteralControl("</br>Create Alert Text:</br>"));
            createAlertText = new TextBox();
            toolPanePanel.Controls.Add(createAlertText);

            toolPanePanel.Controls.Add(new LiteralControl("</br>Delete Alert Text:</br>"));
            deleteAlertText = new TextBox();
            toolPanePanel.Controls.Add(deleteAlertText);


            TagLine productTagLine = new TagLine();
            productTagLine.ProductName = "Quick Alert";
            productTagLine.ProductId = "88";
            toolPanePanel.Controls.Add(productTagLine);

            base.CreateChildControls();
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            QuickAlertWebPart webPart = WebPartToEdit as QuickAlertWebPart;

            if (webPart != null)
            {
                webPart.ListName = listDropDown.SelectedItem.Text;
                webPart.AlertFrequency = (SPAlertFrequency)Enum.Parse(typeof(SPAlertFrequency), frequencyDropDown.SelectedValue);
                webPart.ChangeType = (SPEventType)Enum.Parse(typeof(SPEventType), changeTypeDropDown.SelectedValue);
                webPart.CreateAlertText = createAlertText.Text;
                webPart.DeleteAlertText = deleteAlertText.Text;
            }
            return true;

        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            QuickAlertWebPart webPart = WebPartToEdit as QuickAlertWebPart;

            if (webPart != null)
            {
                if (listDropDown.Items.FindByText(webPart.ListName) != null)
                {
                    listDropDown.ClearSelection();
                    listDropDown.Items.FindByText(webPart.ListName).Selected = true;
                }

                if (frequencyDropDown.Items.FindByValue(webPart.AlertFrequency.ToString()) != null)
                {
                    frequencyDropDown.ClearSelection();
                    frequencyDropDown.Items.FindByValue(webPart.AlertFrequency.ToString()).Selected = true;
                }

                if (changeTypeDropDown.Items.FindByValue(webPart.ChangeType.ToString()) != null)
                {
                    changeTypeDropDown.ClearSelection();
                    changeTypeDropDown.Items.FindByValue(webPart.ChangeType.ToString()).Selected = true;
                }

                createAlertText.Text = webPart.CreateAlertText;
                deleteAlertText.Text = webPart.DeleteAlertText;
            }
        }
    }
}